﻿using ProductMonitor.OpCommand;
using ProductMonitor.UserControls;
using ProductMonitor.ViewModels;
using ProductMonitor.Views;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        readonly MainWindowVM mainWindowVM = new();

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = mainWindowVM;
        }

        /// <summary>
        /// 显示车间详情页
        /// </summary>
        private void ShowDetailUC() 
        {
            WorkShopDetailUC workShopDetailUC = new();
            mainWindowVM.MonitorUC = workShopDetailUC;
            //动画效果
            ThicknessAnimation thicknessAnimation = new(
                    new Thickness(0, 50, 0, -50),
                    new Thickness(0, 0, 0, 0),
                    new TimeSpan(0, 0, 0, 0, 400));

            //透明度
            DoubleAnimation doubleAnimation = new(
                0,
                1,
                new TimeSpan(0, 0, 0, 0, 400));

            Storyboard.SetTarget(thicknessAnimation, workShopDetailUC);
            Storyboard.SetTarget(doubleAnimation, workShopDetailUC);

            Storyboard.SetTargetProperty(thicknessAnimation,new PropertyPath(nameof(Margin)));
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(nameof(Opacity)));

            Storyboard storyboard = new();
            storyboard.Children.Add(thicknessAnimation);
            storyboard.Children.Add(doubleAnimation);
            storyboard.Begin();
        }

        /// <summary>
        /// 返回首页
        /// </summary>
        private void GoBack() 
        {
            MonitorUC monitorUC = new();
            mainWindowVM.MonitorUC = monitorUC;
            //动画效果
            ThicknessAnimation thicknessAnimation = new(
                    new Thickness(0, 50, 0, -50),
                    new Thickness(0, 0, 0, 0),
                    new TimeSpan(0, 0, 0, 0, 400));

            //透明度
            DoubleAnimation doubleAnimation = new(
                0,
                1,
                new TimeSpan(0, 0, 0, 0, 400));

            Storyboard.SetTarget(thicknessAnimation, monitorUC);
            Storyboard.SetTarget(doubleAnimation, monitorUC);

            Storyboard.SetTargetProperty(thicknessAnimation, new PropertyPath(nameof(Margin)));
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(nameof(Opacity)));

            Storyboard storyboard = new();
            storyboard.Children.Add(thicknessAnimation);
            storyboard.Children.Add(doubleAnimation);
            storyboard.Begin();
        }

        /// <summary>
        /// 展示详情命令
        /// </summary>
        public Command ShowDetailCMD { 
            get 
            {
                return new Command(ShowDetailUC);    
            } 
        }

        /// <summary>
        /// 返回首页命令
        /// </summary>
        public Command GoBackCMD
        {
            get
            {
                return new Command(GoBack);
            }
        }

        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnMin(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClose(object sender, RoutedEventArgs e)
        {
            this.Close();
            //Environment.Exit(0);
        }

        /// <summary>
        /// 展示设置页面
        /// </summary>
        private void ShowSettings() 
        {
            var settingsWindow = new SettingsWindow() { Owner = this };
            settingsWindow.ShowDialog();
        }

        public Command ShowSettingsCmd 
        {
            get 
            {
                return new Command(ShowSettings);
            }
        }
    }
}